<?php
/**
 * RUBIK IT Solution & Development ltd.
 *
 * PROJECT		: [Project name]
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * @author			nguyennt <nguyennt@rubik.com.vn>
 * @access			[public/private]
 * @package		 [package name]
 *
 * Created on	:Sep 19, 2008
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
define ( "HTML_INFO_PICTURE_TYPE", 1 );
define ( "HTML_INFO_CBX_TYPE", 2 );
define ( "HTML_INFO_TFL_TYPE", 3 );
define ( "HTML_INFO_CHECKB_TYPE", 4 );
define ( "HTML_INFO_DATE_TYPE", 5 );
define ( "HTML_INFO_HIDDEN_TYPE", 6 );
define ( "HTML_INFO_TEXTAREA_POOL", 7 );
define ( "HTML_INFO_TEXTAREA_RICH", 8 );
define ( "HTML_INFO_FILE_FORM", 9 );
define ( "HTML_INFO_PICTURE_FORM", 10 );
define ( "HTML_INFO_FLASH_TYPE", 11 );

class HtmlFieldInfo {
	var $_name;
	var $_ruleLink;
	var $_columnType;
	var $_isColumnInfo = false;
	/**
	 * Enter description here...
	 *
	 * @var SysDatabase
	 */
	var $_db;
	var $_nameAndId;
	var $_firstNameAndId;
	var $_dirContainImage;
	var $_arrayValue;
	var $_arrayDefaultValue;
	var $_extAttribute;
	
	function __construct($NameAndId = '', $tblColumnType = HTML_INFO_PICTURE_TYPE, $sDirContain = '', $extAttribute = '', $TblColumnLinkRule = '', $db = '') {
		if (empty ( $db )) {
			$this->_db = SysDatabase::getInstance ();
		} else {
			$this->_db = $db;
		}
		$this->_nameAndId = $NameAndId;
		$this->_firstNameAndId = $NameAndId;
		$this->_ruleLink = $TblColumnLinkRule;
		$this->_columnType = $tblColumnType;
		if ($sDirContain) {
			$this->_dirContainImage = SYS_ROOT_URL . $sDirContain;
		}
		$this->_extAttribute = $extAttribute;
		$this->_isColumnInfo = true;
	
	}
	
	/**
	 * Set Prefix for Mutilanguage
	 *
	 * @param str $prefix
	 */
	function setPrefixToName($prefix) {
		$this->_nameAndId = $prefix . $this->_firstNameAndId;
	}
	
	function setAttribute($extAttribute = '') {
		$this->_extAttribute = $extAttribute;
	}
	
	/**
	 * set extension
	 *
	 * @param int $type // type of value ex: Array,SQL
	 * @param String $ext  //extAttribute
	 */
	function setExt($type = '', $SqlOrArrayValue = '') {
		switch (strtoupper ( $type )) {
			case 'SQL' :
				$db = $this->_db;
				$db->setQuery ( $SqlOrArrayValue );
				$result = $db->loadRowList ();
				foreach ( $result as $record ) {
					$this->_arrayValue [] = $record;
				}
				break;
			case 'ARRAY' :
				$this->_arrayValue = $SqlOrArrayValue;
				break;
		}
	}
	
	/**
	 * set directory Contain image or
	 *
	 * @param String $dirContain
	 */
	function setDirContainImage($dirContain = '') {
		if ($this->_columnType == HTML_INFO_PICTURE_TYPE) {
			$this->_dirContainImage = $dirContain;
		}
	}
	
	/**
	 * generate HTML 
	 *
	 * @param String $data
	 * @return String
	 */
	function generateHTML($data = '') {
		$name = $this->_nameAndId;
		switch ($this->_columnType) {
			case HTML_INFO_PICTURE_TYPE :								
				$dirContain = $this->_dirContainImage;
				$linkRule = $this->_ruleLink;
				if (file_exists ( SYS_ROOT_PATH . $data )) {
					$arrayInfoFile = pathinfo ( SYS_ROOT_PATH . $data );
					$fileThumbName = $dirContain . $arrayInfoFile ['filename'] . "_thumb." . $arrayInfoFile ['extension'];									
				}
				$html = "<img name=\"{$name}\" id=\"{$name}\" src=\"{$fileThumbName}\" {$this->_extAttribute} onclick=\"{$linkRule}\"/>";
				break;
			case HTML_INFO_CBX_TYPE :				
				$arrayvalue = $this->_arrayValue;
				$html = "<select name=\"{$name}\" {$this->_extAttribute}>";
				if ($this->_extAttribute) {
					$html .= "<option value=\"\" selected>--Please select--</option>";
				}
				foreach ( $arrayvalue as $record ) {
					$selected = '';
					if ($record [0] == $data) {
						$selected = "selected";
					}
					$html .= "<option value=\"{$record[0]}\" {$selected}>$record[1]</option>";
				}
				$html .= "</select>";
				break;
			case HTML_INFO_DATE_TYPE :
				RCoreImporter::cls( "html.HtmlDateTextBoxControl" );
				$date = new HtmlDateTextBoxControl ( $name, $data );
				$html = $date->createInputHtml ();
				break;
			case HTML_INFO_HIDDEN_TYPE :
				$arrayvalue = $this->_arrayValue;
				$found = false;
				if (is_array ( $arrayvalue )) {
					foreach ( $arrayvalue as $record ) {
						if ($record [0] == $data) {
							$found = true;
							$data = $record [1];
							break;
						}
					}
				}
				if ($found) {
					$html = "<input type=\"hidden\" id=\"{$name}\" name=\"{$name}\" value=\"{$data}\" />";
				} else {
					$html = "<input type=\"text\" readonly=\"readonly\" value=\"{$data}\" />";
				}
				break;
			case HTML_INFO_TEXTAREA_POOL :
				$arrayvalue = $this->_arrayValue;
				$data = htmlspecialchars($data);
				$html = "<textarea cols=\"{$arrayvalue[0]}\" rows=\"{$arrayvalue[1]}\" id=\"{$name}\" name=\"{$name}\">{$data}</textarea>";
				break;
			case HTML_INFO_TEXTAREA_RICH :
				RCoreImporter::cls( "html.HtmlEditor" );
				$eContent = new HtmlEditor ( $name );
				$arrayvalue = $this->_arrayValue;
				$html = $eContent->createEditor ( $data );
				
				break;
			case HTML_INFO_FILE_FORM :
				RCoreImporter::cls( "html.UploadControl" );
				$fileForm = new UploadControl ( $name, $this->_dirContainImage, $data, $this->_extAttribute );
				$html = '<div style="width: 300px;">'.$fileForm->createUploadFormHtml ( 50 ).'</div>';
				break;
			case HTML_INFO_PICTURE_FORM :
				if (file_exists ( SYS_ROOT_PATH . $data )) {
					$arrayInfoFile = explode ( '/', $data );
					$lastNum = count ( $arrayInfoFile ) - 1;
					$arrayInfoFile [$lastNum] = 'thumb/' . $arrayInfoFile [$lastNum];
					$fileThumbName = implode ( '/', $arrayInfoFile );
					$fileName = SYS_ROOT_URL . $fileThumbName;
					$html = "<img name=\"{$name}\" id=\"{$name}\" src=\"{$fileName}\" />";
				}
				break;
			case HTML_INFO_CHECKB_TYPE:															
				if(!empty($data)) {
					$isChecked = " checked=\"checked\"";									
				}
				$html = "<input type='checkbox' name=\"{$name}\" {$isChecked} value='1'";				
				$html .= "</input>";
				break;
			
			case HTML_INFO_FLASH_TYPE:															
				if (file_exists ( SYS_ROOT_PATH . $data ))
				{					
					
					$extAttribute = $this->_extAttribute;
					RCoreImporter::cls( "helper.RHtmlHelper" );
					$width = $extAttribute["width"];
					$height = $extAttribute["height"];
					$html= RHtmlHelper::createFlashObject(SYS_ROOT_URL . $data, $width, $height);	 																													
				}
				break;
											
		}
		return $html;
	}
}
?>